package com.zcj.test.hourglassanim;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.zcj.test.hourglassanim.dots.AnimatedView;
import com.zcj.test.hourglassanim.dots.AnimatorPlayer;
import com.zcj.test.hourglassanim.dots.HesitateInterpolator;
import com.zcj.test.hourglassanim.dots.ProgressLayout;
import com.zcj.test.hourglassanim.wave.WaveView;

public class MainActivity extends AppCompatActivity implements Handler.Callback {
    private WaveView waveView_top;
    private WaveView waveView_bottom;
    private Handler handler;
    int progress = 100;
    private ProgressLayout progressLayout_green;
    private AnimatedView[] spots_green;
    private AnimatorPlayer animator_green;
    private int size = 8;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        waveView_bottom = (WaveView) findViewById(R.id.waveView_bottom);
        waveView_top = (WaveView) findViewById(R.id.waveView_top);
        progressLayout_green = (ProgressLayout) findViewById(R.id.waveView_progress);
        handler = new Handler(this);
        setProgress();
        initProgressGreen();
    }

    private void initProgressGreen() {
        if (spots_green == null) {
            int size = progressLayout_green.getSpotsCount();
            spots_green = new AnimatedView[size];
            for (int i = 0; i < spots_green.length; i++) {
                AnimatedView v = new AnimatedView(this);
                v.setBackgroundResource(R.drawable.shap_dots_green);
                v.setTarget(5);
                v.setYFactor(1f);
                progressLayout_green.addView(v, size, size);
                spots_green[i] = v;
                play();
            }

        }
    }

    private void play() {
        animator_green = new AnimatorPlayer(createAnimationsGreen(1000, 100 - progress));
        animator_green.play();
    }

    private Animator[] createAnimationsGreen(int duration, int num) {
        Animator[] animators = new Animator[size];
        for (int i = 0; i < spots_green.length; i++) {
            Animator move = ObjectAnimator.ofFloat(spots_green[i], "yFactor", 0, num);
            move.setDuration(duration);
            move.setInterpolator(new HesitateInterpolator());
            move.setStartDelay(100 * i);
            animators[i] = move;
        }
        return animators;
    }

    private void setProgress() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                handler.sendEmptyMessageDelayed(1, 1000);
            }
        }).start();

    }

    @Override
    public boolean handleMessage(Message message) {
        switch (message.what) {
            case 1:
                progress--;
                waveView_top.setProgress(progress);
                waveView_bottom.setProgress(100 - progress);
                if (progress > 0) {
                    setProgress();
                    animator_green.stop();
                    play();
                } else {
                    progressLayout_green.setVisibility(View.GONE);
                }
                break;
        }
        return true;
    }
}

